﻿/// <reference path="jquery-1.7-vsdoc.js" />
/// <reference path="MyLibrary.js" />

(function (window, undefined) {
    function getXmlHttpObject() {
        var xmlHttp = null;
        try {
            // Firefox, Opera 8.0+, Safari
            xmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            // Internet Explorer
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
    var Ajax = function () {
        var xmlHttp = getXmlHttpObject();
        xmlHttp.source = this;
        this.getXMLHttp = function () {
            return xmlHttp;
        };
    };

    Ajax.prototype = Ajax.fn = {
        constructor: Ajax,
        success: function () {
            var result = new Object();
            result.Text = arguments[0];
            result.Headers = arguments[1];
            return result;
        },
        setDefault: function (url) {
            this.get(url);
            this.setOnReadyStateChange(this.stateChange);
        },
        get: function (url) {
            this.getXMLHttp().open("get", url, true);
        },
        post: function (url) {
            this.getXMLHttp().open("post", url, true);
        },
        setHeader: function (key, value) {
            this.getXMLHttp().setRequestHeader(key, value);
        },
        setOnReadyStateChange: function (fun) {
            this.getXMLHttp().onreadystatechange = fun;
        },
        setData: function (data) {
            var datatemp = "";
            if (typeof data === "object") {
                datatemp = Convert.jsonObjectToString(data);
            } else {
                datatemp = data;
            }
            this.getXMLHttp().send(datatemp);
        },
        isSuccess: function () {
            return this.getXMLHttp().readyState == 4 && this.getXMLHttp().xmlhttp.status == 200;
        },
        stateChange: function (event) {
            var source = this.source;
            var xml = source.getXMLHttp();
            if (xml.readyState == 4) {
                var responseHeaders = xml.getAllResponseHeaders();
                var arrayHeaders = responseHeaders.split(/\n/);
                var objectHeaders = changeArrayToObject(arrayHeaders, ":");
                source.success(xml.responseText, objectHeaders);
            }
        }
    };
    window.A = Ajax;
    window.Ajax = Ajax;
})(window)

function stateChanged(event) {
    if (event.target.readyState == 4) {
        var objectData = JSON.parse(event.target.responseText).d;
        var object = JSON.parse(objectData)
        var responseHeaders = event.target.getAllResponseHeaders();
        var arrayHeaders = responseHeaders.split(/\n/);
        var objectHeaders = changeArrayToObject(arrayHeaders, ":");
        document.getElementById("result").setAttribute("value", object.william);
    }
}


function windowsReady(event) {
    var get = document.getElementById("get");
    var text = document.getElementById("result");
    get.attachEvent("onclick", function () {
        var xmlHttp = GetXmlHttpObject();
        xmlHttp.onreadystatechange = stateChanged;
        xmlHttp.open("Post", "Json.aspx/GetJsonWithParams", true);
        xmlHttp.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
        xmlHttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        var object = new Object();
        object.key = "william";
        object.value = 12;
        var jsonStr = JSON.stringify(object);
        xmlHttp.send(jsonStr);
    });



}
